package com.wjw.service;

import com.wjw.mapper.FacilityDamageTableMapper;
import com.wjw.model.Facility_damage_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Facility_damage_table_view;
import com.wjw.viewmodel.Roadway_table_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FacilityDamageTableService {


    //private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
    //private FacilityDamageTableMapper mapper=sqlsession.getMapper(FacilityDamageTableMapper.class);


    public List<Facility_damage_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        FacilityDamageTableMapper mapper=sqlsession.getMapper(FacilityDamageTableMapper.class);
        try {
            List<Facility_damage_table> all = mapper.findAll();
            List<Facility_damage_table_view> list= new ArrayList<Facility_damage_table_view>();
            for(Facility_damage_table e:all){
                Facility_damage_table_view k = new Facility_damage_table_view();
                k.setEmployee_patrol_name(mapper.findEmployeeById(e.getEmployee_patrol_id()).getEmployee_name());
                k.setEmployee_technology_name(mapper.findEmployeeById(e.getEmployee_technology_id()).getEmployee_name());
                k.setFacility_damage_handle_opinion(e.getFacility_damage_handle_opinion());
                k.setFacility_damage_id(e.getFacility_damage_id());
                k.setFacility_damage_name(e.getFacility_damage_name());
                k.setFacility_damage_ndate(e.getFacility_damage_ndate());
                k.setFacility_damage_position(e.getFacility_damage_position());
                k.setFacility_damage_rdate(e.getFacility_damage_rdate());
                k.setFacility_damage_reason(e.getFacility_damage_reason());
                k.setRoad_basic_name(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Facility_damage_table_view> findByPage(int page,int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        FacilityDamageTableMapper mapper=sqlsession.getMapper(FacilityDamageTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Facility_damage_table> all = mapper.findByPage(start,limit);
            List<Facility_damage_table_view> list= new ArrayList<Facility_damage_table_view>();
            for(Facility_damage_table e:all){
                Facility_damage_table_view k = new Facility_damage_table_view();
                k.setEmployee_patrol_name(mapper.findEmployeeById(e.getEmployee_patrol_id()).getEmployee_name());
                k.setEmployee_technology_name(mapper.findEmployeeById(e.getEmployee_technology_id()).getEmployee_name());
                k.setFacility_damage_handle_opinion(e.getFacility_damage_handle_opinion());
                k.setFacility_damage_id(e.getFacility_damage_id());
                k.setFacility_damage_name(e.getFacility_damage_name());
                k.setFacility_damage_ndate(e.getFacility_damage_ndate());
                k.setFacility_damage_position(e.getFacility_damage_position());
                k.setFacility_damage_rdate(e.getFacility_damage_rdate());
                k.setFacility_damage_reason(e.getFacility_damage_reason());
                k.setRoad_basic_name(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Facility_damage_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        FacilityDamageTableMapper mapper=sqlsession.getMapper(FacilityDamageTableMapper.class);
        try {
            Facility_damage_table k = new Facility_damage_table();
            k.setEmployee_patrol_id(mapper.findEmployeeByName(e.getEmployee_patrol_name()).getEmployee_id());
            k.setEmployee_technology_id(mapper.findEmployeeByName(e.getEmployee_technology_name()).getEmployee_id());
            k.setFacility_damage_handle_opinion(e.getFacility_damage_handle_opinion());
            k.setFacility_damage_id(e.getFacility_damage_id());
            k.setFacility_damage_name(e.getFacility_damage_name());
            k.setFacility_damage_ndate(e.getFacility_damage_ndate());
            k.setFacility_damage_position(e.getFacility_damage_position());
            k.setFacility_damage_rdate(e.getFacility_damage_rdate());
            k.setFacility_damage_reason(e.getFacility_damage_reason());
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name()).getRoad_basic_id());
            mapper.add(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        FacilityDamageTableMapper mapper=sqlsession.getMapper(FacilityDamageTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Facility_damage_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        FacilityDamageTableMapper mapper=sqlsession.getMapper(FacilityDamageTableMapper.class);
        try {
            Facility_damage_table k = new Facility_damage_table();
            k.setEmployee_patrol_id(mapper.findEmployeeByName(e.getEmployee_patrol_name()).getEmployee_id());
            k.setEmployee_technology_id(mapper.findEmployeeByName(e.getEmployee_technology_name()).getEmployee_id());
            k.setFacility_damage_handle_opinion(e.getFacility_damage_handle_opinion());
            k.setFacility_damage_id(e.getFacility_damage_id());
            k.setFacility_damage_name(e.getFacility_damage_name());
            k.setFacility_damage_ndate(e.getFacility_damage_ndate());
            k.setFacility_damage_position(e.getFacility_damage_position());
            k.setFacility_damage_rdate(e.getFacility_damage_rdate());
            k.setFacility_damage_reason(e.getFacility_damage_reason());
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name()).getRoad_basic_id());
            mapper.change(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }
}
